<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
A Bounds-Consistent IC constraint
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial103.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial098.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial105.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc208">14.6</A>&nbsp;&nbsp;A Bounds-Consistent IC constraint</H2>
To show the basic ideas, we will simply reimplement a constraint that
already exists in the <B>ic</B> solver, the inequality constraint.
We want a constraint ge/2 that takes two <B>ic</B> variables (or numbers)
and constrains the first to be greater or equal to the second.<BR>
<BR>
<A NAME="@default377"></A>
The behaviour should be to maintain bounds-consistency:
If we have a goal <TT>ge(X,Y)</TT>, where the domain of <CODE>X is X{1..5}</CODE> and
the domain of <CODE>Y is Y{3..7}</CODE>, we would like the domains to be updated such
that the upper bound of Y gets reduced to 5, and the lower bound of X
gets increased to 3. The following code achieves this:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
ge(X, Y) :-
        get_bounds(X, _, XH),
        get_bounds(Y, YL, _),
        ( var(X),var(Y) -&gt;
            suspend(ge(X,Y), 0, [X-&gt;ic:max, Y-&gt;ic:min])
        ;
            true
        ),
        X #&gt;= YL,    % impose new bounds
        Y #=&lt; XH.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
We have used a single primitive from the low-level interface of the
<B>ic</B> library: <B>get_bounds/3</B>, which extracts the current
domain bounds from a variable. Further, we have used the information
that the library implements trigger conditions called <B>min</B>
and <B>max</B>, which cause a goal to wake up when the lower/upper
bound on an <B>ic</B> variable changes.<BR>
<BR>
Note that we suspend a new instance of the <TT>ge(X,Y)</TT> goal <EM>before</EM>
we impose the new bounds on the variables. This is important when the
constraint is to be used together with other constraints of higher
priority: imposing a bound may immediately wake and execute such
a higher-priority constraint. The higher-priority constraint may then
in turn change one of the bounds that ought to wake ge/2 again.
This only works if ge/2 has already been (re-)suspended at that time.<BR>
<BR>
<HR>
<A HREF="tutorial103.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial098.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial105.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
